package nl.project.system;

import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.GregorianCalendar;

import nl.project.source.Mailer;
import nl.project.source.Message;

public class MailALLtheshit {

	private int counter;
	private GregorianCalendar cal1;
	private GregorianCalendar cal2;
	Mailer mailer = new Mailer();
	
	public MailALLtheshit(Message message){
		if(counter < 400){
	        try {
				mailer.sendMail(message.getReceiverMail(), message.toString());
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        counter++;
	        if(counter == 400){
	        	cal1 = new GregorianCalendar();
	        	Calendar.getInstance();
	        }
        }
        else {
        	cal2 = new GregorianCalendar();
        	Calendar.getInstance();
        	
        	long timeLapse = cal2.getTimeInMillis() - cal1.getTimeInMillis();
        	timeLapse/=1000;
        	if(timeLapse >= 240){
        		counter = 0;
        	}
        }
	}
}
